package pers.zc.activiti.thread.closethread;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.zc.activiti.thread.demo.*;
import pers.zc.activiti.thread.pool.ThreadPool;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @description 关闭正在运行的线程
 * @Author Lin FuYuan
 * @date 2020/4/16
 */
@Service
public class CloseThread {
    @Autowired
    private AsyncDemoTwo asyncDemoTwo;
    @Autowired
    private AsyncDemoOne asyncDemoOne;
    @Autowired
    private FutureClass1 futureClass1;
    @Autowired
    private FutureClass2 futureClass2;


    @Autowired
    private ThreadPool threadPool;
    public static List<Thread> list = new ArrayList<>();
    public void start() throws InterruptedException {
        asyncDemoOne.testOne();
       asyncDemoTwo.testTwo();
    }


    public void stop() throws ExecutionException, InterruptedException {
        for (Thread thread : list) {
            thread.interrupt();
        }
//        for (int i = 0; i < vector.size(); i++) {
//            boolean cancel = vector.get(i).cancel(true);
//            vector.remove(i);
//        }
        System.out.println("active Count "+threadPool.testThreadPool().getActiveCount());
    }


    public void testRunnable(){
        RunnableClass1  cl1 = new RunnableClass1();
        RunnableClass2 cl2 = new RunnableClass2();
        threadPool.testThreadPool();
        Thread thread = new Thread();
        thread.interrupt();
    }




    public  void testFuture(){
        Future<Integer> test1 = futureClass1.test();
        Future<Integer> test2 =futureClass2.test();

        if(test1.isDone()&&test2.isDone()){
            try {
                Integer s = test1.get();
                Integer s1 = test2.get();
                System.out.println(s);
                System.out.println(s1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println(test1.isCancelled());

        System.out.println(test2.isCancelled());

    }



    /**
     * 测试多个线程同时跑, 在最短的时间获取所有线程的结果
     * 可以用  forkjoin 或者  cyclicbarrier
     * @author Lin FuYuan
     * @param
     * @date 2020/4/20
     * @return void
     */
    public void testFutureAdd()  {
        List<Future<Integer>> num = new ArrayList<>();
        Long start = System.currentTimeMillis();
        for (int i = 0; i <10 ; i++) {
            Future<Integer> test = futureClass1.test();
                num.add(test);
        }
        System.out.println("最终结果"+num);

        System.out.println("用时:"+(System.currentTimeMillis()- start));
    }


    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal(0);
        bigDecimal = bigDecimal.add(null);
        System.out.println(bigDecimal);
    }
}
